﻿package mortal.game.view.lotteryDraw
{
    import Message.Public.*;
    import extend.language.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.view.ThanksGiving.command.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class lotteryDrawController extends Controller
    {
        private var _module:lotteryDrawModule;
        private var _activeTipWindow:LotteryDrawActiveTipWindow;
        private var _isShowIcon:Boolean = false;
        private var _t1:int = 0;
        private var _t2:int = 0;

        public function lotteryDrawController()
        {
            return;
        }// end function

        private function ShowIcon(param1:Object = null) : void
        {
            var _loc_2:* = !lotteryDrawIcon.hasInstance() || lotteryDrawIcon.instance.isHide;
            var _loc_3:* = Cache.instance.lotteryDraw.isHideLottery;
            if (_loc_3 != false)
            {
                if (!_loc_2)
                {
                    lotteryDrawIcon.instance.hide();
                }
                this._isShowIcon = false;
                return;
            }
            if (lotteryDrawUtil.checkCanOpen())
            {
                if (_loc_2)
                {
                    lotteryDrawIcon.instance.show();
                    if (Cache.instance.lotteryDraw.isEffectFlag)
                    {
                        lotteryDrawIcon.instance.playEffect();
                    }
                }
                this._isShowIcon = true;
            }
            else
            {
                if (!_loc_2)
                {
                    lotteryDrawIcon.instance.hide();
                }
                this._isShowIcon = false;
            }
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._module == null)
            {
                this._module = new lotteryDrawModule();
                this._module.addEventListener(WindowEvent.SHOW, this.onShow);
                this._module.addEventListener(WindowEvent.CLOSE, this.onClose);
            }
            return this._module;
        }// end function

        private function onShow(event:WindowEvent) : void
        {
            Dispatcher.addEventListener(EventName.LotteryExchangeRequestRecord, this.onRequestRecord);
            Dispatcher.addEventListener(EventName.LotteryDrawInfoUpdate, this.onGetRecord);
            Dispatcher.addEventListener(EventName.LotteryExchangeInfoUpdate, this.onGetRecord);
            return;
        }// end function

        private function onClose(event:WindowEvent) : void
        {
            Dispatcher.removeEventListener(EventName.LotteryExchangeRequestRecord, this.onRequestRecord);
            Dispatcher.removeEventListener(EventName.LotteryDrawInfoUpdate, this.onGetRecord);
            Dispatcher.removeEventListener(EventName.LotteryExchangeInfoUpdate, this.onGetRecord);
            if (this._activeTipWindow != null)
            {
                this._activeTipWindow.hide();
            }
            return;
        }// end function

        private function onBagInfoUpdate(param1:Object = null) : void
        {
            if (lotteryDrawPackageView.hasInstance() && !lotteryDrawPackageView.instance.isHide)
            {
                lotteryDrawPackageView.instance.updateAllItems();
            }
            var _loc_2:* = Cache.instance.pack.lotteryDrawBag.itemLength > 0 ? (true) : (false);
            if (_loc_2 != Cache.instance.lotteryDraw.isModaiEffect)
            {
                Cache.instance.lotteryDraw.isModaiEffect = _loc_2;
                Dispatcher.dispatchEvent(new DataEvent(EventName.LotteryDrawPackageBtnPlayEffect, _loc_2));
            }
            return;
        }// end function

        override protected function initServer() : void
        {
            Dispatcher.addEventListener(EventName.LotteryOpenExchengePanel, this.lotteryOpenExchengePanel);
            Dispatcher.addEventListener(EventName.LotteryDrawRequestRecord, this.onRequestRecord);
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.onRoleLevelUpdate);
            Dispatcher.addEventListener(EventName.LotteryDrawShow, this.onLotteryDrawShow);
            Dispatcher.addEventListener(EventName.LotteryDrawPlay, this.onLotteryDrawPlay);
            Dispatcher.addEventListener(EventName.LotteryDrawGetReward, this.onLotteryDrawGetReward);
            Dispatcher.addEventListener(EventName.LotteryDrawSuccess, this.onLotteryDrawSuccess);
            Dispatcher.addEventListener(EventName.LotteryDrawPackageShow, this.onLotteryDrawPackageShow);
            NetDispatcher.addCmdListener(ServerCommand.LotteryDrawBagInfoUpdate, this.onBagInfoUpdate);
            NetDispatcher.addCmdListener(ServerCommand.LotteryDrawDiamondPoolInfo, this.onDiamondPoolInfoUpdate);
            NetDispatcher.addCmdListener(ServerCommand.LotteryDrawPlaySuccess, this.onPlaySuccess);
            NetDispatcher.addCmdListener(ServerCommand.LotteryDrawHide, this.onLotteryDrawHide);
            Dispatcher.addEventListener(EventName.LoginGameSuccess, this.ShowIcon);
            NetDispatcher.addCmdListener(ServerCommand.VipLevelUp, this.onVipLevelUpdate);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onPackItemChangeHandler);
            return;
        }// end function

        private function onDiamondPoolInfoUpdate(param1:SLotteryPoolMsg) : void
        {
            Cache.instance.lotteryDrawRecord.diamondPoolInfo = param1;
            Dispatcher.dispatchEvent(new Event(EventName.LotteryDrawDiamondPoolUpdate));
            return;
        }// end function

        private function onLotteryDrawPlay(event:DataEvent) : void
        {
            GameProxy.lotteryDraw.play(int(event.data.times), int(event.data.level));
            return;
        }// end function

        private function onLotteryDrawGetReward(event:DataEvent) : void
        {
            GameProxy.lotteryDraw.getReward(event.data as Array);
            return;
        }// end function

        private function onPlaySuccess(param1:SLotteryClientRecord) : void
        {
            var _loc_2:Point = null;
            var _loc_3:Array = null;
            var _loc_4:Dictionary = null;
            var _loc_5:Dictionary = null;
            var _loc_6:Boolean = false;
            var _loc_7:int = 0;
            var _loc_8:Object = null;
            var _loc_9:Object = null;
            if (!this._module || this._module.isHide)
            {
                return;
            }
            if (param1.lotteryType == 0)
            {
                GameProxy.lotteryExchange.requestMyRecord(param1.level);
                GameProxy.lotteryExchange.requestBAreaRecord(param1.level);
            }
            else
            {
                _loc_2 = this._module.localToGlobal(new Point(228 + 100, 394 - 82));
                _loc_3 = [];
                _loc_4 = param1.lotteryItems;
                _loc_5 = param1.lotteryGold;
                _loc_6 = false;
                _loc_7 = 0;
                for (_loc_8 in _loc_4)
                {
                    
                    _loc_3.push({type:0, value:_loc_8});
                }
                for (_loc_9 in _loc_5)
                {
                    
                    _loc_7 = _loc_7 + _loc_5[_loc_9];
                    _loc_6 = true;
                }
                GameProxy.lotteryDraw.requestMyRecode(param1.level);
                GameProxy.lotteryDraw.requestBAreaRecode(param1.level);
                GameProxy.lotteryDraw.requestRareRecord(param1.level);
                if (_loc_6)
                {
                    lotteryDrawDiamondWin.instance.show();
                    lotteryDrawDiamondWin.instance.data = {num:_loc_7, rewards:_loc_3, startPos:_loc_2};
                }
                if (_loc_3.length > 0)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.LotteryDrawRewardFlyToModai, {rewards:_loc_3, startPos:_loc_2}));
                }
            }
            return;
        }// end function

        private function onLotteryDrawSuccess(event:DataEvent) : void
        {
            return;
        }// end function

        private function onLotteryDrawPackageShow(event:Event) : void
        {
            if (!lotteryDrawPackageView.hasInstance() || lotteryDrawPackageView.instance.isHide)
            {
                lotteryDrawPackageView.instance.show();
            }
            else
            {
                lotteryDrawPackageView.instance.hide();
            }
            return;
        }// end function

        private function onRequestRecord(event:DataEvent) : void
        {
            if (int(event.data.type) == lotteryRecordType.Record_Lottery_Mine)
            {
                GameProxy.lotteryDraw.requestMyRecode(int(event.data.level));
            }
            else if (int(event.data.type) == lotteryRecordType.Record_Lottery_BArea)
            {
                GameProxy.lotteryDraw.requestBAreaRecode(int(event.data.level));
            }
            else if (int(event.data.type) == lotteryRecordType.Record_Lottery_Rare)
            {
                GameProxy.lotteryDraw.requestRareRecord(int(event.data.level));
            }
            else if (int(event.data.type) == lotteryRecordType.Record_Exchange_Mine)
            {
                GameProxy.lotteryExchange.requestMyRecord(int(event.data.level));
            }
            else if (int(event.data.type) == lotteryRecordType.Record_Exchange_BArea)
            {
                GameProxy.lotteryExchange.requestBAreaRecord(int(event.data.level));
            }
            return;
        }// end function

        private function onGetRecord(event:DataEvent) : void
        {
            if (event.data.type == lotteryRecordType.Record_Lottery_Mine)
            {
                Cache.instance.lotteryDrawRecord.myRecordInfo = event.data.info as Array;
                if (this._module && !this._module.isHide)
                {
                    this._module.updateMyRecord(int(event.data.boxLevel));
                }
            }
            else if (event.data.type == lotteryRecordType.Record_Lottery_BArea)
            {
                Cache.instance.lotteryDrawRecord.bAreaRecordInfo = event.data.info as Array;
                if (this._module && !this._module.isHide)
                {
                    this._module.updateBAreaRecord(int(event.data.boxLevel));
                }
            }
            else if (event.data.type == lotteryRecordType.Record_Lottery_Rare)
            {
                Cache.instance.lotteryDrawRecord.rareRecordInfo = event.data.info as Array;
                if (this._module && !this._module.isHide)
                {
                    this._module.updateRareRecord(int(event.data.boxLevel));
                }
            }
            else if (event.data.type == lotteryRecordType.Record_Exchange_Mine)
            {
                Cache.instance.lotteryExchangeRecord.myRecordInfo = event.data.info as Array;
                if (this._module && !this._module.isHide)
                {
                    this._module.updateMyExchangeRecord(int(event.data.boxLevel));
                }
            }
            else if (event.data.type == lotteryRecordType.Record_Exchange_BArea)
            {
                Cache.instance.lotteryExchangeRecord.bAreaRecordInfo = event.data.info as Array;
                if (this._module && !this._module.isHide)
                {
                    this._module.updateBAreaExchangeRecord(int(event.data.boxLevel));
                }
            }
            return;
        }// end function

        private function onLotteryDrawShow(event:DataEvent) : void
        {
            if (!lotteryDrawUtil.checkLevel())
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(90423, String(lotteryDrawUtil.needLevel)));
                return;
            }
            if (!lotteryDrawUtil.checkVipType())
            {
                MsgManager.showRollTipsMsg(Language.getStringByParam(90424, String(lotteryDrawUtil.needVipLevel)));
                return;
            }
            Cache.instance.lotteryDraw.isEffectFlag = false;
            lotteryDrawIcon.instance.stopEffect();
            if (ThanksGivingUtil2.isInActive && !PulseSharedObject.isTodayNotTips(TodayNoTipsConst.LotteryDrawActiveTip))
            {
                if (this._activeTipWindow == null)
                {
                    this._activeTipWindow = LotteryDrawActiveTipWindow.instance;
                }
                this._activeTipWindow.show();
            }
            else if (this._activeTipWindow != null)
            {
                this._activeTipWindow.hide();
            }
            if (!isViewShow)
            {
                if (event.data)
                {
                    lotteryDrawModule.openBoxLevel = int(event.data);
                }
                view.show();
            }
            else
            {
                view.hide();
            }
            return;
        }// end function

        private function onRoleLevelUpdate(param1:Object) : void
        {
            if (!this._isShowIcon)
            {
                this.ShowIcon();
            }
            return;
        }// end function

        private function onVipLevelUpdate(param1:Object) : void
        {
            if (!this._isShowIcon)
            {
                this.ShowIcon();
            }
            return;
        }// end function

        private function onLotteryDrawHide(param1:Boolean) : void
        {
            if (param1 == true)
            {
                Cache.instance.lotteryDraw.isHideLottery = true;
            }
            else
            {
                Cache.instance.lotteryDraw.isHideLottery = false;
            }
            this.ShowIcon();
            return;
        }// end function

        private function lotteryOpenExchengePanel(event:DataEvent) : void
        {
            if (ThanksGivingUtil2.isInActive)
            {
                if (!this.isViewShow)
                {
                    view.show();
                }
                this._module.lotteryOpenExchengePanel();
            }
            else
            {
                MsgManager.showRollTipsMsg(Language.getString(35641), MsgRollTipsType.msgRollTips3);
            }
            return;
        }// end function

        private function onPackItemChangeHandler(param1:Object) : void
        {
            if (isViewShow)
            {
                this._module.updateActivePropNum();
            }
            return;
        }// end function

    }
}
